package com.lyhengtongwl.zqsnews.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.zqsnews.widget.CountTimer;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class CheckOldMobileActivity extends BaseActivity {
    private String smsKey = "";//发送短信返回的key
    CountTimer countTimer;
    @BindView(R.id.et_phone)//手机号
            EditText et_phone;
    @BindView(R.id.et_phoneCode)//手机验证码
            EditText et_phoneCode;
    @BindView(R.id.tv_getcode)//获取验证码
            TextView tvGetcode;
    @BindView(R.id.tv_login)
    TextView tv_login;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    private  String phone;
    private String codeid;

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setListen() {
        et_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBindStatus();
            }
        });
        et_phoneCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBindStatus();
            }
        });
    }

    private void setBindStatus() {
        String myPhone = et_phone.getText().toString();
        String mphoneCode = et_phoneCode.getText().toString();
        if (TextUtils.isEmpty(myPhone) || TextUtils.isEmpty(mphoneCode)) {
            tv_login.setEnabled(false);
        } else {
            tv_login.setEnabled(true);
        }
    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    public int intiLayout() {
        return R.layout.activity_checkold_mobile;
    }

    @Override
    public void initView() {
        ivTitleRight.setVisibility(View.GONE);
        new TitleBuilder(this).setTitleText("")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });

        countTimer = new CountTimer(tvGetcode);
    }

    @Override
    public void initData() {
        phone=SPUtils.get(App.getContext(),"mobile","")+"";
        et_phone.setText(phone.replace(phone.substring(3, 7), "****"));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countTimer != null) {
            countTimer.cancel();
            tvGetcode.setEnabled(true);
            tvGetcode.setText("获取验证码");
        }
    }


    @OnClick({R.id.tv_getcode, R.id.tv_login})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_getcode://获取验证码
                    sendVerify();
                break;
            case R.id.tv_login://检测当前手机号是否已经绑定
                checkOldMobile();
                break;
        }
    }


    //用户登录

    /**
     * 获取验证码
     */
    private void sendVerify() {
        countTimer.start();
        Task.getApiService().getPhoneCode(phone).enqueue(new MyCallback<NewsBaseEntity>(CheckOldMobileActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                if ("1".equals(response.body().getCode())) {
                    NewsToastUtils.showToast(CheckOldMobileActivity.this, "验证码：" + response.body().getMsg());
                    String jsonStr = JSONObject.toJSONString(response.body().getData());
                    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
                    codeid = jsonObject.getString("codeid");
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    /**
     * 检测当前手机号是否绑定
     */
    private void checkOldMobile() {
        int userId = Integer.parseInt(SPUtils.get(this, "userId", "0") + "");
        String code = et_phoneCode.getText().toString();
        Task.getApiService().checkOldMobile(userId, phone, code,codeid).enqueue(new MyCallback<NewsBaseEntity>(CheckOldMobileActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                try {
                    if ("1".equals(response.body().getCode())) {
                        startActivity(new Intent(CheckOldMobileActivity.this, NewsChangePhoneActivity.class));
                        finish();
                    } else {
                        Toast.makeText(CheckOldMobileActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
